//===============================================================================
// Microsoft patterns & practices
// Smart Client Software Factory 2010
//===============================================================================
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================
//----------------------------------------------------------------------------------------
// patterns & practices - Smart Client Software Factory - Guidance Package
//
// This file was generated by the "Add CAB Module" recipe.
//
// This class contains placeholder methods for the common module initialization 
// tasks, such as adding services, or user-interface element
//
// For more information see: 
// ms-help://MS.VSCC.v90/MS.VSIPCC.v90/ms.practices.scsf.2008apr/SCSF/html/03-01-010-How_to_Create_Smart_Client_Solutions.htm
//
// Latest version of this Guidance Package: http://go.microsoft.com/fwlink/?LinkId=62182
//----------------------------------------------------------------------------------------

using System;
using System.Windows.Forms;
using Microsoft.Practices.CompositeUI;
using Microsoft.Practices.CompositeUI.Commands;
using QuickStart.Infrastructure.Interface;
using Microsoft.Practices.SmartClient.DisconnectedAgent;
using Microsoft.Practices.SmartClient.EnterpriseLibrary;
using Microsoft.Practices.CompositeUI.EventBroker;
using QuickStart.Infrastructure.Interface.Constants;
using Microsoft.Practices.SmartClient.ConnectionMonitor;

namespace QuickStart.Infrastructure.Module
{
    public class ModuleController : WorkItemController
    {
        private ToolStripStatusLabel _connectivityStatusLabel;
        private IConnectionMonitor _connectionMonitor;

        public override void Run()
        {
            ExtendStatusStrip();
            AddServices();
        }

        private void ExtendStatusStrip()
        {
            UIExtensionSite status = WorkItem.UIExtensionSites[UIExtensionSiteNames.MainStatus];
            _connectivityStatusLabel = new ToolStripStatusLabel();
            _connectivityStatusLabel.BorderStyle = Border3DStyle.Sunken;
            _connectivityStatusLabel.Spring = true;
            _connectivityStatusLabel.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            status.Add(_connectivityStatusLabel);
        }

        private void AddServices()
        {
            // Set up request manager
            RequestManager requestManager = DatabaseRequestManagerIntializer.Initialize();
            requestManager.StartAutomaticDispatch();

            // Monitor network status changes
            _connectionMonitor = requestManager.ConnectionMonitor;
            _connectionMonitor.ConnectionStatusChanged += new EventHandler(connectionMonitor_ConnectionStatusChanged);
            
            // Show current connectivity state
            UpdateConnectivityStatusLabel(_connectionMonitor.IsConnected);
            
            // Add the request queue to the Root WorkItem. This queue will be used
            // by service agents to enqueue requests.
            WorkItem.RootWorkItem.Services.Add(requestManager.RequestQueue);

            // Add the connection monitor to the Root WorkItem. It will be used
            // to determine connectivity status and provide feedback to the user
            // accordingly.
            WorkItem.RootWorkItem.Services.Add(_connectionMonitor);
        }

        private void connectionMonitor_ConnectionStatusChanged(object sender, EventArgs e)
        {
            UpdateConnectivityStatusLabel(_connectionMonitor.IsConnected);
        }

        private void UpdateConnectivityStatusLabel(bool connected)
        {
            _connectivityStatusLabel.Text = connected ? "Online" : "Offline";
        }
    }
}
